import Vue from 'vue'
import messageBox from './MessageBox'

export let MessageBox = (function() {
  let defaults = {
    title: '',
    content: '',
    cancel: '',
    ok: '',
    handleCancel: null,
    handleOk: null
  }

  var MyComponent = Vue.extend(messageBox)

  return function(opts) {
    for(var attr in opts) {
      defaults[attr] = opts[attr]
    }

    let vm = new MyComponent({
      el: document.createElement('div'),
      data: {...defaults },
      methods: {
        cancelFn() { // 点击取消按钮调用
          defaults.handleCancel && defaults.handleCancel.call(this)
          document.body.removeChild(vm.$el)
        },
        okFn() { // 点击确定按钮调用
          defaults.handleOk && defaults.handleOk.call(this)
          document.body.removeChild(vm.$el)
        }
      }
    })
    document.body.appendChild(vm.$el)
  }
})()
